/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models.Card;
import myTest.MYTest;
import cstAndEnum.*;
import java.util.ArrayList;
import java.util.List;
import models.Game;
import models.Player;
import models.Skill.Skill;

/**
 * 牌
 * @author Invince
 */
public class Card {
/*----------Attributs-------------------*/
    /**
     * 牌的id
     */
    protected int id;
    /**
     * 牌的花色
     */
    protected COLOR color;
    /**
     * 牌的数值 A, 2, 3,... J, Q, K
     */
    protected int number;
    /**
     * 牌的名字
     */
    protected String name;
    /**
     * 牌的技能
     */
    protected List<Skill> listSkill;

/*----------Public functions------------*/
//   public boolean produce(){//出牌
//        return false;
//   }

    /**
     * 给牌添加技能
     * @param skill
     * @return
     */
    public boolean addSkill(Skill skill){
        String r="Card--addSkill:";
        boolean rb;
        if (listSkill.contains(skill)){
           rb= false;
           r="Failed: "+r+this.name+" addSkill "+skill.getName();
        }
        else {
            rb=listSkill.add(skill);
            r+=this.name+" addSkill "+skill.getName();
        }
        if(MYTest.TEST)
            System.out.println(r);
        return rb;
    }

    /**
     * 删除牌的技能
     * @param skill
     * @return
     */
    public boolean deleteSkill(Skill skill){
        String r="Card: deleteSkill:";
        boolean rb;
        if (!listSkill.contains(skill)){
          rb= false;
          r="Failed: "+r+this.name+" deleteSkill "+skill.getName();
        }
        else {
           rb=listSkill.remove(skill);
           r+=this.name+" deleteSkill "+skill.getName();
        }
        if(MYTest.TEST)
            System.out.println(r);
        return rb;
    }

    /**
     * 比较牌的大小
     * @param card
     * @return
     */
    public COMPARE_RESULT compareCard(Card card){
        int rb=this.number-card.getNumber();
        String r="Card--compareCard:";
        if(rb>0){
           r+=this.name+" greater than "+card.getName();
           MYTest.println(r);
           return COMPARE_RESULT.GT;
        }
        if(rb==0){
           r+=this.name+" equals to "+card.getName();
           MYTest.println(r);
           return COMPARE_RESULT.EQ;
        }
        r+=this.name+" litter than "+card.getName();
        MYTest.println(r);
        return COMPARE_RESULT.LT;  //Means not greater than nor equal
    }

    /**
     * 牌是否为红色
     * @return
     */
    public boolean isRed(){
        if(this.color==COLOR.HEART)
           return true;
        if(this.color==COLOR.DIAMOND)
           return true;
        return false;
    }

    /**
     * 牌是否为黑色
     * @return
     */
    public boolean isBlack(){
        return !this.isRed();
    }

    /**
     * 返回牌是黑还是红
     * @return
     */
    public RED_OR_BLACK redORblack(){
        if(this.isRed())
           return RED_OR_BLACK.RED;
        return RED_OR_BLACK.BLACK;
    }

    /**
     * 使用牌的技能
     * @param skill
     * @param targets
     * @param source
     * @return
     */
    public boolean castSkill(Skill skill,List<Player> targets,Player source,Game game){
        if(this.listSkill.contains(skill)){
           return skill.cast(targets,source,game);
        }
        else
            return false;
    }

   @Override
   public boolean equals(Object obj){
       if(obj==null){
           return false;
       }
       if(obj.getClass()!=this.getClass()){
           return false;
       }
       Card cObj=(Card) obj;
       return (this.id==cObj.id);
   }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + this.id;
        return hash;
    }



/*----------Constructor----------------*/
    /**
     * Constructor
     * @param color
     * @param number
     * @param name
     */
    public Card(int id,COLOR color, int number, String name) {
        this.id=id;
        this.color = color;
        this.number = number;
        this.name = name;
        this.listSkill=new ArrayList<Skill>();
    }

/*---------Gets and sets---------------*///Sets here is not used
    public COLOR getColor() {
        return color;
    }

    public void setColor(COLOR color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public List<Skill> getListSkill() {
        return listSkill;
    }

    public void setListSkill(List<Skill> listSkill) {
        this.listSkill = listSkill;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}
